//
//  HDTakeMoneyViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/18.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDTakeMoneyViewController.h"

@interface HDTakeMoneyViewController ()
@property(nonatomic,strong)UIView *headerView;
@property(nonatomic,strong)HDTextField *sumOfMoneyField;
@property(nonatomic,strong)UIView *infoView;
@property(nonatomic,strong)HDTextField *bankNameField;
@property(nonatomic,strong)HDTextField *bankAccountField;
@property(nonatomic,strong)HDTextField *userNameField;
@property(nonatomic,strong)HDTextField *securityCodeField;
@property(nonatomic,strong)HDTextField *phoneNumberField;
@property(nonatomic,strong)HDTextField *verifyCodeField;
@property(nonatomic,strong)UIButton *securityBtn;
@property(nonatomic,strong)UIButton *verifyCodeBtn;
@property(nonatomic,strong)UIButton *confirmBtn;
@property CGPoint activePoint;
@property NSTimer* time;
@end

@implementation HDTakeMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kContentViewColor;
    self.contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, APPFRAME.size.width, APPFRAME.size.height-44)];
    [self.view addSubview:self.contentView];
    self.contentView.contentSize = CGSizeMake(APPFRAME.size.width, self.contentView.height);
    self.contentView.bounces = NO;
    [self.contentView addSubview:self.headerView];
    [self.contentView addSubview:self.infoView];
    self.contentView.contentSize = CGSizeMake(APPFRAME.size.width, self.headerView.height+self.infoView.height+10);
    self.contentView.scrollEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.contentView addGestureRecognizer:tapGesture];
    [self registerForKeyboardNotifications];
}


- (void)confirmAction:(UIButton *)sender
{
    if (![HDUtil checkPrice:self.sumOfMoneyField.text]) {
        [netEngine showErrorAlertWithMsg:@"金额格式不正确"];
        return;
    }
    if (![HDUtil checkName:self.bankNameField.text]) {
        [netEngine showErrorAlertWithMsg:@"开户行名称格式不正确"];
        return;
    }
    if (![HDUtil checkBankCardId:self.bankAccountField.text]) {
        [netEngine showErrorAlertWithMsg:@"银行账号格式不正确"];
        return;
    }
    if (![HDUtil checkName:self.userNameField.text]) {
        [netEngine showErrorAlertWithMsg:@"账号持有人姓名格式不正确"];
        return;
    }
    NSDictionary* params=@{
                           @"coach":netEngine.coachInfo.m_id,
                           @"mobile_no":netEngine.coachInfo.mobile_no,
                           @"verify_code":self.verifyCodeField.text,
                           @"name":self.userNameField.text,
                            @"money":self.sumOfMoneyField.text,
                            @"pay_bank":self.bankNameField.text,
                            @"pay_account":self.bankAccountField.text,
                           };
    [netEngine RequestWithdrawWithTarget:self.VctrID hudView:self.contentView Data:params];
    
}

-(void)updateViewController:(NSNotification *)note
{
    NSDictionary* dic=note.userInfo;
    if ([dic objectForKey:Receiver]==self.VctrID)
    {
        if (dic[@"Withdraw"]!=nil)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else if (dic[@"SecurityCode"]!=nil) {
            self.securityCodeField.text = dic[@"SecurityCode"];
        }
        else if (dic[@"VerifyCode"]!=nil)
        {
            if (self.time)
            {
                [self.time invalidate];
                self.time=nil;
            }
            self.verifyCodeBtn.tag=60;
            self.verifyCodeBtn.enabled=NO;
            self.time=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }
    }
}

- (void)checkInputFinished
{
    self.confirmBtn.enabled = (self.sumOfMoneyField.text.length&&self.bankNameField.text.length&&self.bankAccountField.text.length&&self.userNameField.text.length&&self.securityCodeField.text.length&&self.phoneNumberField.text.length&&self.verifyCodeField.text.length);
}

- (void)tapAction
{
    [self.view endEditing:YES];
}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(textBeginEditing:)
                                                name:UITextViewTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(textBeginEditing:)
                                                name:UITextFieldTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(checkInputFinished)
                                                name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)textBeginEditing:(NSNotification*)aNotification
{
    UIView* sender=[aNotification object];
    CGRect rect=sender.frame;
    rect.origin.y+=rect.size.height;
    self.activePoint=[[sender superview] convertPoint:rect.origin toView:self.contentView];
    self.activePoint=[self.contentView convertPoint:self.activePoint toView:self.contentView];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    kbSize.height+=30;
    
    float hiddenH=self.contentView.frame.size.height-(self.contentView.frame.size.height-kbSize.height);
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,hiddenH, 0.0);
    self.contentView.contentInset = contentInsets;
    self.contentView.scrollIndicatorInsets = contentInsets;
    float activeH=self.contentView.frame.size.height-self.activePoint.y;
    if(kbSize.height>activeH)
    {
        float dif=activeH-kbSize.height;
        CGPoint scrollPoint = CGPointMake(0.0,0-dif);
        [self.contentView setContentOffset:scrollPoint animated:YES];
    }
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [UIView animateWithDuration:0.3 animations:^{
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        self.contentView.contentInset = contentInsets;
        self.contentView.scrollIndicatorInsets = contentInsets;
    }];
}

- (void)getCodeAction:(UIButton *)sender
{
    if (sender==self.securityBtn) {
        [netEngine RequestSecurityCodeWithTarget:self.VctrID hudView:self.contentView];
    }
    else if (sender==self.verifyCodeBtn)
    {
        [netEngine RequestVerifyCodeWithTarget:self.VctrID hudView:self.contentView Data:@{@"mobile_no":[HDUtil check:netEngine.coachInfo.mobile_no]}];
    }
}

-(void)timerAction
{
    self.verifyCodeBtn.tag-=1;
    if (self.verifyCodeBtn.tag<=0)
    {
        self.verifyCodeBtn.enabled=YES;
        [self.verifyCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
    else
    {
        [self.verifyCodeBtn setTitle:[NSString stringWithFormat:@"%zis",self.verifyCodeBtn.tag] forState:UIControlStateNormal];
    }
}

#pragma mark - getters
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 70)];
        _headerView.backgroundColor = [UIColor whiteColor];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 19, 32, 32)];
        image.contentMode = UIViewContentModeScaleAspectFit;
        image.layer.masksToBounds = YES;
        image.image = [UIImage imageNamed:@"人民币"];
        [_headerView addSubview:image];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(image.right+5, 15, 60, 40)];
        label.textColor = kDarkGrayColor;
        label.font = [UIFont systemFontOfSize:15];
        label.text = @"提现金额";
        [_headerView addSubview:label];
        self.sumOfMoneyField = [[HDTextField alloc] initWithFrame:CGRectMake(label.right+5, 15, APPFRAME.size.width-120, 40) isRightAngle:YES];
        self.sumOfMoneyField.placeholder = @"0.00";
        self.sumOfMoneyField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
        [_headerView addSubview:self.sumOfMoneyField];
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 70, APPFRAME.size.width, 0.5)];
        line.image = [UIImage imageNamed:@"dot"];
        [_headerView addSubview:line];
    }
    return _headerView;
}

- (UIView *)infoView
{
    if (!_infoView) {
        _infoView = [[UIView alloc] initWithFrame:CGRectMake(0, self.headerView.bottom+10, APPFRAME.size.width, APPFRAME.size.height-54-_headerView.height)];
        _infoView.backgroundColor = [UIColor whiteColor];
        UILabel *bankNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 100, 40)];
        bankNameLabel.textColor = kDarkGrayColor;
        bankNameLabel.font = [UIFont systemFontOfSize:14];
        bankNameLabel.numberOfLines = 0;
        bankNameLabel.text = @"提现到\n开户行名称";
        [bankNameLabel sizeToFit];
        [_infoView addSubview:bankNameLabel];
        self.bankNameField = [[HDTextField alloc] initWithFrame:CGRectMake(10, bankNameLabel.bottom+10, APPFRAME.size.width-20, 40) isRightAngle:YES];
        self.bankNameField.placeholder = @"请输入开户行名称";
        [_infoView addSubview:self.bankNameField];
        UILabel *bankAccountLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.bankNameField.bottom+5, 60, 20)];
        bankAccountLabel.textColor = kDarkGrayColor;
        bankAccountLabel.font = [UIFont systemFontOfSize:14];
        bankAccountLabel.text = @"银行账号";
        [bankAccountLabel sizeToFit];
        [_infoView addSubview:bankAccountLabel];
        self.bankAccountField = [[HDTextField alloc] initWithFrame:CGRectMake(10, bankAccountLabel.bottom+5, APPFRAME.size.width-20, 40) isRightAngle:YES];
        self.bankAccountField.placeholder = @"请输入银行账号";
        [_infoView addSubview:self.bankAccountField];
        UILabel *userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.bankAccountField.bottom+5, 100, 20)];
        userNameLabel.textColor = kDarkGrayColor;
        userNameLabel.font = [UIFont systemFontOfSize:14];
        userNameLabel.text = @"账号持有人姓名";
        [userNameLabel sizeToFit];
        [_infoView addSubview:userNameLabel];
        self.userNameField = [[HDTextField alloc] initWithFrame:CGRectMake(10, userNameLabel.bottom+5, APPFRAME.size.width-20, 40) isRightAngle:YES];
        self.userNameField.placeholder = @"请输入账号持有人姓名";
        [_infoView addSubview:self.userNameField];
        self.securityCodeField = [[HDTextField alloc] initWithFrame:CGRectMake(10, self.userNameField.bottom+10, APPFRAME.size.width-100, 40) isRightAngle:YES];
        self.securityCodeField.placeholder = @"请点击获取安全码按钮";
        self.securityCodeField.userInteractionEnabled = NO;
        [_infoView addSubview:self.securityCodeField];
        
        self.securityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.securityBtn.frame = CGRectMake(APPFRAME.size.width-85, self.securityCodeField.top+5, 75, 30);
        [self.securityBtn setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
        [self.securityBtn setTitle:@"获取安全码" forState:UIControlStateNormal];
        [self.securityBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.securityBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        self.securityBtn.layer.cornerRadius = 5;
        [self.securityBtn addTarget:self action:@selector(getCodeAction:) forControlEvents:UIControlEventTouchUpInside];
        [_infoView addSubview:self.securityBtn];
        
        self.phoneNumberField = [[HDTextField alloc] initWithFrame:CGRectMake(10, self.securityCodeField.bottom+10, APPFRAME.size.width-100, 40) isRightAngle:YES];
        self.phoneNumberField.userInteractionEnabled = NO;
        self.phoneNumberField.text = [HDUtil check:netEngine.coachInfo.mobile_no];
        [_infoView addSubview:self.phoneNumberField];
        
        self.verifyCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.verifyCodeBtn.frame = CGRectMake(APPFRAME.size.width-85, self.phoneNumberField.top+5, 75, 30);
        [self.verifyCodeBtn setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
        [self.verifyCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.verifyCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.verifyCodeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        self.verifyCodeBtn.layer.cornerRadius = 5;
        [self.verifyCodeBtn addTarget:self action:@selector(getCodeAction:) forControlEvents:UIControlEventTouchUpInside];
        [_infoView addSubview:self.verifyCodeBtn];
        
        self.verifyCodeField = [[HDTextField alloc] initWithFrame:CGRectMake(10, self.phoneNumberField.bottom+10, APPFRAME.size.width/2.0+30, 40) isRightAngle:YES];
        self.verifyCodeField.placeholder = @"输入验证码";
        [_infoView addSubview:self.verifyCodeField];

        self.confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.confirmBtn.frame = CGRectMake(10, self.verifyCodeField.bottom+20, APPFRAME.size.width-20, 44);
        [self.confirmBtn setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
        [self.confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
        self.confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
        self.confirmBtn.enabled = NO;
        [_infoView addSubview:self.confirmBtn];
        
        _infoView.height = self.confirmBtn.bottom + 20;
    }
    return _infoView;
}

@end
